import 'package:t3_data_service/biz_data_source/map/address/city_pick/model/city_pick_models.dart';
import 'package:t3_data_service/biz_data_source/map/address/poi_search/poi_search.dart';
import 'package:t3_data_service/data_provider/map_sctx/t3_map_sctx_provider.dart';
import 'package:t3_foundation/t3_foundation.dart';
import 'address_select_config.dart';
import 'address_select_enums.dart';

class MapSelectConfig extends AddressSelectConfig {
  /// 图选开城校验参数：菜单id，
  String? menuId;
  /// 图选开城校验参数：开城数据类型
  CheckInitType? checkInitType;

  /// 展示围栏类型
  List<MapSpotConfigType> spotConfigType = const [
    MapSpotConfigType.invalidFence, MapSpotConfigType.fence, MapSpotConfigType.personal
  ];

  /// 是否选取允许围栏外点
  /// 只有allowMoveOutFence为true时，此字端才有效
  /// 当此字端为false时，会对移动到围栏外的点进行校验，并将校验结果显示到大头针上
  /// 只有通过校验的点才允许返给业务
  var allowSelectPoiOutFence = true;

  /// 页面消失动作
  /// 搜索进入图选是: pop
  /// 直接进入图选是:dismiss, 因为要present图选页面
  var disappearMode = MapDisappearMode.pop;
  /// 页面来源类型
  var mapSelectSource = T3MapSelectSource.normal;
  /// 进入图选的类型:展示不同UI逻辑
  var mode = T3MapSelectMode.common;

  String? keyword;

  POIModel? poi;
  /// 易混淆的点
  List<POIModel>? confusionPoints;

  /// 门店信息
  String? enterpriseUuid;
  String? storeUuid;

  /// 是否展示顶部搜索框与取消按钮，只展示返回按钮
  var showSearchHeader = true;

  MapSelectConfig({
    required super.location,
    super.originCoor,
    super.sceneRuleId,
    super.refererPageTitle,
    super.allowMoveOutFence = true,
    super.pointType = T3POIType.start,
    super.isUserActionDisabledForEnd = false,
    super.isUserActionDisabledForVia = false,
    super.style = T3POIStyle.normal,
    super.cityScope = CityScope.opened,
    super.showMapSelectedFooter = false,
    super.showConfusionButton = true,
    super.showBackButton = false,
    super.shouldCheckTableSelectedPoi = true,
    this.menuId,
    this.checkInitType,
    this.spotConfigType = const [MapSpotConfigType.invalidFence, MapSpotConfigType.fence, MapSpotConfigType.personal],
    this.allowSelectPoiOutFence = true,
    this.disappearMode = MapDisappearMode.pop,
    this.mapSelectSource = T3MapSelectSource.normal,
    this.mode = T3MapSelectMode.common,
    this.keyword,
    this.poi,
    this.confusionPoints,
    this.enterpriseUuid,
    this.storeUuid,
    this.showSearchHeader = true,
  });

  @override
  Map<String, dynamic> toJson() {
    var json = super.toJson();
    json['menuId'] = menuId;
    json['checkInitType'] = checkInitType;
    json['spotConfigType'] = spotConfigType.map((e) => e.value).toList();
    json['allowSelectPoiOutFence'] = allowSelectPoiOutFence;
    json['disappearMode'] = disappearMode.name;
    json['mapSelectSource'] = mapSelectSource.name;
    json['mode'] = mode.value;
    json['keyword'] = keyword;
    json['poi'] = poi?.toJson();
    json['confusionPoints'] = confusionPoints?.map((e) => e.toJson()).toList();
    json['enterpriseUuid'] = enterpriseUuid;
    json['storeUuid'] = storeUuid;
    json['showSearchHeader'] = showSearchHeader;
    return json;
  }

  static MapSelectConfig fromJSON(Map<dynamic, dynamic> json) {
    List<MapSpotConfigType> spotConfigType = [];
    var spotConfigTypeList = json['spotConfigType'];
    if (spotConfigTypeList is List<dynamic>?) {
      for (var item in spotConfigTypeList ?? []) {
        var type = MapSpotConfigType.fromJson(item);
        if (type != null) {
          spotConfigType.add(type);
        }
      }
    }

    List<POIModel> confusionPoints = [];
    var confusionPointsList = json['confusionPoints'];
    if (confusionPointsList is List?) {
      for (var item in confusionPointsList ?? []) {
        var type = POIModel.fromJson(item);
        confusionPoints.add(type);
      }
    }

    POIModel? poi;
    var poiJson = json['poi'];
    if (poiJson is Map? && poiJson != null) {
      poi = POIModel.fromJson(poiJson);
    }

    var config = MapSelectConfig(
      location: $T3POILocationDecode(json['location']),
      originCoor: LatLng.fromJson(json['originCoor']),
      allowMoveOutFence: BoolUtils.dynamicToBool(json['allowMoveOutFence'], true),
      pointType: T3POIType.fromJson(IntUtils.dynamicToNullInt(json['pointType'])),
      isUserActionDisabledForEnd: BoolUtils.dynamicToBool(json['isUserActionDisabledForEnd']),
      isUserActionDisabledForVia: BoolUtils.dynamicToBool(json['isUserActionDisabledForVia']),
      style: T3POIStyle.fromJson(IntUtils.dynamicToNullInt(json['style'])),
      cityScope: CityScopeExtension.initWithRawValue(IntUtils.dynamicToNullInt(json['cityScope'])) ?? CityScope.opened,
      showMapSelectedFooter: BoolUtils.dynamicToBool(json['showMapSelectedFooter']),
      showConfusionButton: BoolUtils.dynamicToBool(json['showConfusionButton'],true),
      showBackButton: BoolUtils.dynamicToBool(json['showBackButton']),
      sceneRuleId: StringUtils.dynamicToString(json['sceneRuleId']),
      refererPageTitle: StringUtils.dynamicToString(json['refererPageTitle']),
      shouldCheckTableSelectedPoi: BoolUtils.dynamicToBool(json['shouldCheckTableSelectedPoi'], true),
      menuId: StringUtils.dynamicToString(json['menuId']),
      checkInitType: CheckInitType.fromJson(StringUtils.dynamicToString(json['checkInitType'])),
      spotConfigType: spotConfigType,
      allowSelectPoiOutFence: BoolUtils.dynamicToBool(json['allowSelectPoiOutFence'], true),
      disappearMode: MapDisappearMode.fromJson(StringUtils.dynamicToString(json['disappearMode'])) ?? MapDisappearMode.pop,
      mapSelectSource: T3MapSelectSource.fromJson(StringUtils.dynamicToString(json['mapSelectSource'])) ?? T3MapSelectSource.normal,
      mode: T3MapSelectMode.fromJson(IntUtils.dynamicToNullInt(json['mode'])),
      keyword: StringUtils.dynamicToString(json['keyword']),
      poi: poi,
      confusionPoints: confusionPoints,
      enterpriseUuid: StringUtils.dynamicToString(json['enterpriseUuid']),
      storeUuid: StringUtils.dynamicToString(json['storeUuid']),
      showSearchHeader: BoolUtils.dynamicToBool(json['showSearchHeader'], true)
    );
    config.mapType = T3MapType.fromJson(IntUtils.dynamicToNullInt(json['mapType']));
    return config;
  }
}